<template>
	<view>
		<view class='productList'>

			<!-- <view class='search bg-color acea-row row-between-wrapper' style="margin-top: 0px;z-index: 999;">
			
				<view class='input acea-row row-between-wrapper' style="background-color: #0081FF;">
					<input placeholder='搜索商品名称' placeholder-class='placeholder' confirm-type='search' name="search"
						:value='where.keyword' @confirm="searchSubmit"></input>
				</view>
				<view class='iconfont' :class='is_switch==true?"icon-pailie":"icon-tupianpailie"' @click='Changswitch'>
				</view>
			</view> -->

			<view class="navbar" :style="{position:headerPosition,top:headerTop}">
				<view class="nav-item" :class="{current: filterIndex === 0}" @click="tabClick(0)">
					综合
				</view>
				<view class="nav-item" :class="{current: filterIndex === 1}" @click="tabClick(1)">
					新品
				</view>
				<view class="nav-item" :class="{current: filterIndex === 2}" @click="tabClick(2)">
					推荐
				</view>
			</view>


			<view class='list acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
				<view class='item' :class='is_switch==true?"":"on"' hover-class='none'
					v-for="(item,index) in productList" :key="index" @click="prodctDetail(item)">
					<view class='pictrue' :class='is_switch==true?"":"on"'>
						<image :src='item.icon' :class='is_switch==true?"":"on"'></image>
					</view>
					<view class='text' :class='is_switch==true?"":"on"'>
						<view class='name line1'>{{item.commodityName}}</view>
						<view class='money font-color' :class='is_switch==true?"":"on"'>￥<text
								class='num'>{{priceFilter(item.sellingPrice)}}</text></view>
						<view class='vip acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
						
							
							<view>已售:{{item.salesVolume}}{{item.unit_name}}</view>
						</view>
					</view>
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if='hasNext'>
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
			</view>


		</view>
		<view class='noCommodity' v-if="productList.length==0&& where.page == 1&& loading== false">
			<view class='pictrue'>
				<image src='../../static/images/noShopper.png'></image>
			</view>

		</view>

	</view>
</template>

<script>
	import {
		getCommodityList
	} from '@/server/product.js';


	import {
		mapGetters
	} from "vuex";

	export default {
		//computed: mapGetters(['uid']),
		components: {
		
		},
		data() {
			return {
				productList: [],
				is_switch: true,
				filterIndex: 0,
				where: {
					query: "",
					commodityCategoryId: '',
					page: 1,
					limit: 100,
					recommend: 2,
					commodityCategoryId: "614fee328854cd1a4b6d9d06",
				},
				price: 0,
				stock: 0,
				nows: false,
				recom: false,
				hasNext: true,
				loading: false,
				loadTitle: '加载更多',
				//	title: true,
				//hostProduct: [],
				//	hotPage: 1,
				//	hotLimit: 10,
				//	hotScroll: false,

			};
		},
		onLoad: function(options) {
			this.loading = false;
			this.where.commodityCategoryId = options.id
			this.getCommodityList();
		},
		methods: {
			// 去详情页
			prodctDetail(item) {
				console.log(item)
				uni.navigateTo({
		
					url: `/pages/product/product?type=1&id=`+item.id
				})
			},
			priceFilter(n = 0) {
				let that = this;
				if (typeof n == "string") {
					n = parseFloat(n);
				}
				if (that.amountType === 0) {
					return n.toFixed(2);
				} else {
					return (n / 100).toFixed(2);
				}
			},
			Changswitch: function() {
				let that = this;
				that.is_switch = !that.is_switch
			},
			searchSubmit: function(e) {

				let that = this;
				that.$set(that.where, 'query', e.detail.value);

				that.$set(that.where, 'page', 1)
				that.getCommodityList()

			},
			getPrice(skuList){
				console.log(skuList)
				let min
				if (skuList && skuList.length > 0) {
					let valueArr = [];
					skuList.map((skuItem, index) => {
						console.log(index+":"+skuItem)
				
						valueArr.push(skuItem.sellingPrice);
					});
					 min = this.priceFilter(Math.min(...valueArr));
			
			 	}
				return min;
			},
			priceFilter(n = 0) {
				let that = this;
				if (typeof n == "string") {
					n = parseFloat(n);
				}
				if (that.amountType === 0) {
					return n.toFixed(2);
				} else {
					return (n / 100).toFixed(2);
				}
			},
			//筛选点击
			tabClick(index) {
				if (this.filterIndex === index && index !== 2) {
					return;
				}
				this.filterIndex = index;
				if (index === 2) {
					this.where.recommend = 0 //推荐
					this.priceOrder = this.priceOrder === 1 ? 2 : 1;
				} else {
					if (index === 0) {
						this.where.recommend = 2 //所有
					} else if (index === 1) {
						this.where.recommend = 1 //新品
					}
					this.priceOrder = 0;
				}
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.getCommodityList()
			},
			/**
			 * 获取商品列表
			 */
			getCommodityList: function() {
				uni.showLoading({
					title: '加载中'
				});
				this.loading = true;
				getCommodityList(this.where).then(data => {
					uni.hideLoading();
					this.loading = false;
					this.productList = data.list
					this.productList.push(data.list[0])
					this.productList.push(data.list[1])
					this.productList.push(data.list[2])
					this.productList.push(data.list[0])
					this.productList.push(data.list[1])
					this.productList.push(data.list[2])
					this.productList.push(data.list[0])
					this.productList.push(data.list[1])
					this.productList.push(data.list[2])
					this.productList.push(data.list[0])
					this.productList.push(data.list[1])
				
					this.hasNext = data.hasNext
				})
			},
			//点击事件处理
			set_where: function(e) {
				switch (e) {
					case 1:
						// #ifdef H5
						return history.back();
						// #endif
						// #ifndef H5
						return uni.navigateBack({
							delta: 1,
						})
						// #endif
						break;
					case 2:
						if (this.price == 0) this.price = 1;
						else if (this.price == 1) this.price = 2;
						else if (this.price == 2) this.price = 0;
						this.stock = 0;
						break;
					case 3:
						this.recom = true
						this.nows = false
						break;
					case 4:
						this.nows = true
						this.recom = false
						break;
				}

				this.$set(this.where, 'page', 1);

			},
			//设置where条件
			setWhere: function() {
				if (this.price == 0) this.where.priceOrder = '';
				else if (this.price == 1) this.where.priceOrder = 'asc';
				else if (this.price == 2) this.where.priceOrder = 'desc';
				if (this.stock == 0) this.where.salesOrder = '';
				else if (this.stock == 1) this.where.salesOrder = 'asc';
				else if (this.stock == 2) this.where.salesOrder = 'desc';
				this.where.news = this.nows ? 1 : 0;
			},

		},
		onPullDownRefresh() {
			//this.getCommodityList()
		},
		onReachBottom() {
			this.where.page++
			console.log("**********")
			console.log(this.where)
			if (this.productList.length > 0 && this.hasNext) {
				getCommodityList(this.where).then(data => {
					this.loading = false;
					for(var i=0;i<data.list.length;i++){
							this.productList.push(data.list[i]) 
					}
					this.hasNext = data.hasNext
				})
				//	this.get_product_list();
			} else {
          
			}

		}
	}
</script>

<style scoped lang="scss">
	// page{
	// 	background-color: #FFFFFF;
	// }
	.productList .search {
		width: 100%;
		height: 106rpx;
		padding-left: 23rpx;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 9;
	}

	.productList .search .input {
		width: 560rpx;
		height: 60rpx;
		background-color: #fff;
		border-radius: 50rpx;
		padding: 0 10rpx;
		text-align: center;
		box-sizing: border-box;
	}

	.productList .search .input input {
		width: 510rpx;
		height: 100%;
		font-size: 26rpx;
	}

	.productList .search .input .placeholder {
		color: #999;
	}

	.productList .search .input .iconfont {
		font-size: 35rpx;
		color: #555;
	}

	.productList .search .icon-pailie,
	.productList .search .icon-tupianpailie {
		color: #fff;
		width: 62rpx;
		font-size: 40rpx;
		height: 86rpx;
		line-height: 86rpx;
	}

	.productList .nav {
		height: 86rpx;
		color: #454545;
		position: fixed;
		left: 0;
		width: 100%;
		font-size: 28rpx;
		background-color: #fff;
		margin-top: 86rpx;
		top: 0;
		z-index: 9;
	}

	.productList .nav .item {
		width: 25%;
		text-align: center;
	}

	.productList .nav .item.font-color {
		font-weight: bold;
	}

	.productList .nav .item image {
		width: 15rpx;
		height: 19rpx;
		margin-left: 10rpx;
	}

	.productList .list {
		padding: 0 20rpx;
		margin-top: 80rpx;
	}

	.productList .list.on {
		background-color: #fff;
		border-top: 1px solid #f6f6f6;
	}

	.productList .list .item {
		width: 345rpx;
		margin-top: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}

	.productList .list .item.on {
		width: 100%;
		display: flex;
		border-bottom: 1rpx solid #f6f6f6;
		padding: 30rpx 0;
		margin: 0;
	}

	.productList .list .item .pictrue {
		position: relative;
		width: 100%;
		height: 345rpx;
	}

	.productList .list .item .pictrue.on {
		width: 180rpx;
		height: 180rpx;
	}

	.productList .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 20rpx 20rpx 0 0;
	}

	.productList .list .item .pictrue image.on {
		border-radius: 6rpx;
	}

	.productList .list .item .text {
		padding: 20rpx 17rpx 26rpx 17rpx;
		font-size: 30rpx;
		color: #222;
	}

	.productList .list .item .text.on {
		width: 508rpx;
		padding: 0 0 0 22rpx;
	}

	.productList .list .item .text .money {
		font-size: 26rpx;
		font-weight: bold;
		margin-top: 8rpx;
	}

	.productList .list .item .text .money.on {
		margin-top: 50rpx;
	}

	.productList .list .item .text .money .num {
		font-size: 34rpx;
	}

	.productList .list .item .text .vip {
		font-size: 22rpx;
		color: #aaa;
		margin-top: 7rpx;
	}

	.productList .list .item .text .vip.on {
		margin-top: 12rpx;
	}

	.productList .list .item .text .vip .vip-money {
		font-size: 24rpx;
		color: #282828;
		font-weight: bold;
	}

	.productList .list .item .text .vip .vip-money image {
		width: 46rpx;
		height: 21rpx;
		margin-left: 4rpx;
	}

	.noCommodity {

		padding-bottom: 30rpx;
	}
</style>
<style lang="scss">
	page,
	.content {
		background: $page-color-base;
	}

	.content {
		padding-top: 96upx;
	}

	.navbar {
		position: fixed;
		left: 0;
		top: var(--window-top);
		display: flex;
		width: 100%;
		height: 80upx;
		background: #fff;
		box-shadow: 0 2upx 10upx rgba(0, 0, 0, .06);
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30upx;
			color: $font-color-dark;
			position: relative;

			&.current {
				color: $base-color;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 70upx;
					height: 0;
					border-bottom: 1upx solid $base-color;
				}
			}
		}

		.p-box {
			display: flex;
			flex-direction: column;

			.yticon {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 14upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #888;

				&.active {
					color: $base-color;
				}
			}

			.xia {
				transform: scaleY(-1);
			}
		}

		.cate-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 44upx;

			&:after {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}

	/* 分类 */
	.cate-mask {
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 95;
		transition: .3s;

		.cate-content {
			width: 630upx;
			height: 100%;
			background: #fff;
			float: right;
			transform: translateX(100%);
			transition: .3s;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.cate-content {
				transform: translateX(0);
			}
		}
	}

	.cate-list {
		display: flex;
		flex-direction: column;
		height: 100%;

		.cate-item {
			display: flex;
			align-items: center;
			height: 90upx;
			padding-left: 30upx;
			font-size: 28upx;
			color: #555;
			position: relative;
		}

		.two {
			height: 64upx;
			color: #303133;
			font-size: 30upx;
			background: #f8f8f8;
		}

		.active {
			color: $base-color;
		}
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;

		.goods-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}

		.price-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $font-color-light;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 26upx;
			}
		}
	}
</style>
